﻿using CuaHangLego.Bussiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CuaHangLego.Webform.Controls
{
    public partial class UserForm : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserAccess.IsLogin())
                txtUserHello.Text = "Chào bạn <b>" + UserAccess.currentUser.HoTen + "</b>";
            LoginFm.Visible = false;
            ButtonClose.Visible = false;
            LoginFm.FailureText = "Sai tài khoản hoặc mật khẩu!";
            NoLoginPanel.Visible = !UserAccess.IsLogin();
            LoggedPanel.Visible = UserAccess.IsLogin();
            if (Session["cart"] != null) txtSoLuong.Text = string.Format("Bạn có {0} sản phẩm trong giỏ hàng", (Session["cart"] as IList<int>).Count);
        }

        protected void BtLogin_Command(object sender, CommandEventArgs e)
        {
            LoginFm.Visible = true;
            ButtonClose.Visible = true;
        }

        protected void LoginFm_LoginError(object sender, EventArgs e)
        {

        }

        protected void ButtonClose_Command(object sender, CommandEventArgs e)
        {
            LoginFm.Visible = false;
            ButtonClose.Visible = false;
        }

        protected void LoginFm_Authenticate(object sender, AuthenticateEventArgs e)
        {
            UserAccess.Login(LoginFm.UserName, Encoding.Default.GetString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(LoginFm.Password))));
            LoginFm.Visible = !UserAccess.IsLogin();
            NoLoginPanel.Visible = !UserAccess.IsLogin();
            LoggedPanel.Visible = UserAccess.IsLogin();
            if (UserAccess.IsLogin())
                txtUserHello.Text = "Chào bạn <b>" + UserAccess.currentUser.HoTen + "</b>";
        }

        protected void LoginFm_LoggedIn(object sender, EventArgs e)
        {
        }

        protected void ButtonLogout_Command(object sender, CommandEventArgs e)
        {
            UserAccess.Logout();
            NoLoginPanel.Visible = !UserAccess.IsLogin();
            LoggedPanel.Visible = UserAccess.IsLogin();
        }

        protected void ButtonViewShoppingCart_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("~/ShoppingCart.aspx");
        }
    }
}